package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsAdminRoleRelationMapper;
import com.itheima.mapper.UmsRoleMapper;
import com.itheima.pojo.UmsRole;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@DubboService
public class UmsRoleImpl implements UmsRoleApi {

    @Autowired
    private UmsRoleMapper mapper;

    @Autowired
    private UmsAdminRoleRelationMapper umsAdminRoleRelationMapper;

    @Override
    public List<UmsRole> getByIds(List<Long> adminRoleIds) {

        List<UmsRole> umsRoles = mapper.selectBatchIds(adminRoleIds);
        return umsRoles;

    }

    //添加角色
    @Override
    public boolean save(UmsRole umsRole) {

        boolean flag = false;

        List<UmsRole> umsRoleList = listAll();

        for (UmsRole role : umsRoleList) {
            if (StringUtils.pathEquals(role.getName(),umsRole.getName())){
                flag = true;
                return flag;
            }
        }

        LocalDateTime localDateTime = LocalDateTime.now();
        umsRole.setCreateTime(localDateTime);
        mapper.insert(umsRole);

        return flag;
    }

    //修改角色
    @Override
    public boolean update(Integer id, UmsRole umsRole) {

        boolean flag = false;

        List<UmsRole> umsRoleList = listAll();
        for (UmsRole role : umsRoleList) {
            if (StringUtils.pathEquals(role.getName(),umsRole.getName()) && !Objects.equals(role.getId(), umsRole.getId())){
                flag = true;
                return flag;
            }
        }

        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRole::getId, id);
        mapper.update(umsRole,lqw);
        return flag;
    }

    //修改角色状态
    @Override
    public void updateStatus(Integer id, Integer status) {
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRole::getId,id);
        UmsRole umsRole = mapper.selectOne(lqw);
        if (ObjectUtil.isNotNull(umsRole)){
            umsRole.setStatus(status);
            update(id,umsRole);
        }
    }

    //删除角色
    @Override
    public void delete(ArrayList<Integer> ids) {
            mapper.deleteBatchIds(ids);
    }

    //分页+模糊查询
    @Override
    public IPage<UmsRole> list(Integer pageNum, Integer pageSize, String keyword) {
        IPage<UmsRole> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(!ObjectUtil.isEmpty(keyword),UmsRole::getName,keyword);
        return mapper.selectPage(page,lqw);
    }

    @Override
    public UmsRole findById(Long roleId) {
        UmsRole role = mapper.selectById(roleId);
        return role;
    }

    @Override
    public List<UmsRole> selectAll() {
        List<UmsRole> roles = mapper.selectList(new LambdaQueryWrapper<>());
        return roles;
    }

    @Override
    public void setCount(Long roleId, int count) {
        UmsRole role=new UmsRole();
        role.setAdminCount(count);
        mapper.update(role,new LambdaQueryWrapper<UmsRole>().eq(UmsRole::getId,roleId));
    }

    @Override
    public List<UmsRole> listAll() {
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.ne(UmsRole::getId,0);
        List<UmsRole> umsRoleList = mapper.selectList(lqw);
        return umsRoleList;
    }


}
